<?php

namespace app\guild\controller;

use app\common\model\GuildManagerModel;
use app\common\model\UserModel;
use app\common\QcloudSTS;
use think\Controller;
use think\exception\HttpResponseException;
use think\facade\Cookie;
use think\facade\Request;
use think\Response;
use think\Validate;

class BaseController extends Controller {

    // 成员信息
    protected $guild_manager;
    // 代理信息
    protected $guild_info;
    // 代理id
    protected $guild_id;
    // 超管信息
    protected $super_tube;
    // 当前登录经纪人id
    protected $manager_id;
    // 角色id
    protected $role_id;
    // 是否有最高权限 0 没有 1有
    protected $hight_auth = 0;

    /**
     * 参数校验规则
     */
    protected $rules = [];

    protected $OPENSSL_KEY = 'meet';

    /**
     * 初始化操作
     */
    protected function initialize() {
        $token     = Cookie::get("guild_token");
        $guild_mid = Cookie::get("guild_mid");
        if (!$guild_mid || !$token) {
            $this->redirect(url('login/redirectpage'));
        }
        $this->guild_manager = GuildManagerModel::with('guild')->where(['id' => $guild_mid, 'token' => $token, 'status' => 1])->find();
        if (empty($this->guild_manager) || empty($this->guild_manager->guild) || $this->guild_manager->guild->status != 1) {
            $this->clearGolidCookie();
            $this->redirect(url('login/redirectpage'));
        }

        $this->guild_info = $this->guild_manager->guild;
        $this->guild_id   = $this->guild_info->id;
        $this->manager_id = $this->guild_manager->id;
        $this->role_id    = $this->guild_manager->role_id;
        // 是否有最高权限，role_id 1,2是超管和管理员
        $this->hight_auth = in_array($this->role_id, [1, 2]) ? 1 : 0;

        if (!$this->check_params()) {
            $json     = json_encode(array("code" => "1", "msg" => "参数不全"));
            $response = Response::create($json);
            throw new HttpResponseException($response);
        }

        // 超管信息
        $this->super_tube = GuildManagerModel::where(['guild_id' => $this->guild_id, 'role_id' => 1])->find();
    }


    /**
     * 所属代理的主播id
     */
    protected function getGuildUserId() {
        $where = [
            ['guild_id', '=', $this->guild_id],
            ['status', '=', 0]
        ];
        if ($this->hight_auth != 1) {
            // 查询自己及下级经纪人
            $next_manager = self::getNextManager();
            $where[]      = ['guild_manager_id', 'in', $next_manager];
        }
        return UserModel::where($where)->column('id');
    }

    /**
     * 查询自己及下级经纪人
     * @return array
     */
    protected function getNextManager() {
        $where_next = "(FIND_IN_SET({$this->manager_id},path_ids) OR id={$this->manager_id}) AND guild_id={$this->guild_id}";
        return GuildManagerModel::where($where_next)->column('id');
    }

    /**
     * 最高权限判断
     */
    protected function checkHightAuth($is_ajax = 1) {
        if ($this->hight_auth == 1) {
            return;
        }
        if ($is_ajax == 1) {
            echo self::bulidFail('您当前没有权限操作')->send();
            exit;
        }
        $this->error('您暂时没权限访问');
        exit;
    }

    /**
     * 清理cooKie值
     */
    protected function clearGolidCookie() {
        Cookie::delete("guild_mid");
        Cookie::delete("guild_token");
    }

    /**
     * 检测当前控制器和方法是否匹配传递的数组
     *
     * @param array $arr 需要验证权限的数组
     * @return boolean
     */
    private function matchFuc($arr = []) {
        $arr = is_array($arr) ? $arr : explode(',', $arr);
        if (!$arr) {
            return FALSE;
        }
        $arr = array_map('strtolower', $arr);
        // 是否存在
        if (in_array(strtolower(Request::action()), $arr) || in_array('*', $arr)) {
            return TRUE;
        }

        // 没找到匹配
        return FALSE;
    }

    /**
     * [check_params 验证参数  参数过滤]
     * @return [return]      [合格的参数数组]
     */
    public function check_params() {
        if (array_key_exists(Request::action(), $this->rules)) {
            $rule      = $this->rules[Request::action()];
            $validater = new Validate($rule);
            if (!$validater->check(Request::param())) {
                return false;
            }
        }
        return true;
    }

    static function bulidFail($message = "操作失败，请稍后再试") {
        return json(["code" => 1, "msg" => $message]);
    }

    static function bulidDataFail($data = [], $message = "操作失败，请稍后再试") {
        return json(["status" => 1, "msg" => $message, 'data' => $data]);
    }

    static function bulidSuccess($data = [], $message = "操作成功") {
        return json(["code" => 0, "msg" => $message, "data" => $data]);
    }

    static function bulidLoginTimeOut($message = "登录超时，请重新登录") {
        return json(["code" => 2, "msg" => $message]);
    }

    static function echoSuccess($data = ['st' => 0], $msg = "操作成功") {
        echo json_encode(["code" => 0, "data" => $data, 'msg' => $msg]);
    }


    // 公共api  ----------------------------------------------------------------------------------------

    public function signForCos() {
        if ($tempKeys = self::getSignForCos()) {
            return self::bulidSuccess($tempKeys);
        } else {
            return self::bulidFail("获取上传权限失败");
        }
    }

    static function getSignForCos() {
        $configPriModel = getConfigPri();
        $sts            = new QcloudSTS();
        $config         = array(
            'url'             => 'https://sts.tencentcloudapi.com/',
            'domain'          => 'sts.tencentcloudapi.com',
            'proxy'           => '',
            'secretId'        => $configPriModel->qcloud_secretid, // 固定密钥
            'secretKey'       => $configPriModel->qcloud_secretkey, // 固定密钥
            'bucket'          => $configPriModel->cos_bucket, // 换成你的 bucket
            'region'          => $configPriModel->cos_region, // 换成 bucket 所在园区
            'durationSeconds' => 1800, // 密钥有效期
            'allowPrefix'     => "*", // 这里改成允许的路径前缀，可以根据自己网站的用户登录态判断允许上传的具体路径，例子： a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
            // 密钥的权限列表。简单上传和分片需要以下的权限，其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
            'allowActions'    => array(
                // 简单上传
                'name/cos:PutObject',
            )
        );

        // 获取临时密钥，计算签名
        $tempKeys = $sts->getTempKeys($config);
        if ($tempKeys) {
            $tempKeys["bucket"]   = $configPriModel->cos_bucket;
            $tempKeys["region"]   = $configPriModel->cos_region;
            $tempKeys["filename"] = date("YmdHis") . rand(10000, 99999);
            return $tempKeys;
        } else {
            return null;
        }
    }
}